package com.zj.easywebview.util

import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import java.lang.Exception

/**
 * @author: witness
 * created: 2021/10/8
 * desc:
 */
object CommonUtil {
    fun dialogTip(
        context: Context?,
        text: String?,
        posText: String?,
        posListeners: DialogInterface.OnClickListener?,
        negText: String?
    ): Dialog? {
        var posListener = posListeners
        if (posListener == null) {
            posListener = DialogInterface.OnClickListener { dialog, _ -> dialog.dismiss() }
        }
        val dlg = AlertDialog.Builder(context)
            .setTitle("提示")
            .setMessage(text)
            .setPositiveButton(posText, posListener)
            .setNegativeButton(
                negText
            ) { dialog, _ -> dialog.dismiss() }.create()
        if (context is Activity && !context.isFinishing) dlg.show()
        return dlg
    }

    fun getPackageName(context: Context): String {
        try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(
                context.packageName, 0
            )
            return packageInfo.packageName
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }
}